/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Inscription.java
 *
 * Created on 5 janv. 2009, 01:53:32
 */

package fr.miage.shopping.presentation;

import fr.miage.shopping.metier.entite.Client;
import fr.miage.shopping.metier.impl.ClientBusinessServiceImpl;
import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author romain
 */
public class Inscription extends javax.swing.JFrame {

    /** Creates new form Inscription */
    public Inscription() {
        initComponents();
    }

   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lNom = new javax.swing.JLabel();
        tNom = new javax.swing.JTextField();
        lPrenom = new javax.swing.JLabel();
        tPrenom = new javax.swing.JTextField();
        lAdresse = new javax.swing.JLabel();
        tAdresse = new javax.swing.JTextField();
        lVille = new javax.swing.JLabel();
        tVille = new javax.swing.JTextField();
        lTel = new javax.swing.JLabel();
        tTel = new javax.swing.JTextField();
        lEmail = new javax.swing.JLabel();
        tEmail = new javax.swing.JTextField();
        lPass = new javax.swing.JLabel();
        lPass2 = new javax.swing.JLabel();
        bValider = new javax.swing.JButton();
        bAnnuler = new javax.swing.JButton();
        lCp = new javax.swing.JLabel();
        tCp = new javax.swing.JTextField();
        tPass = new javax.swing.JPasswordField();
        tPass2 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Inscription");
        setBackground(new java.awt.Color(51, 51, 255));
        setName("Inscription"); // NOI18N

        lNom.setText("Nom : ");

        lPrenom.setText("Prénom :");

        lAdresse.setText("Adresse :");

        lVille.setText("Ville :");

        lTel.setText("Téléphone :");

        lEmail.setText("Email :");

        lPass.setText("Mot de passe :");

        lPass2.setText("Confirmation mot de passe :");

        bValider.setText("Valider");
        bValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bValiderActionPerformed(evt);
            }
        });

        bAnnuler.setText("Annuler");
        bAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAnnulerActionPerformed(evt);
            }
        });

        lCp.setText("Code postal :");

        tPass.setText("jPasswordField1");
        tPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tPassActionPerformed(evt);
            }
        });

        tPass2.setText("jPasswordField2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lNom, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(lPrenom, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(lAdresse, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(lVille, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(lTel, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(lEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(lPass2, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(bValider, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(40, 40, 40))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lCp, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lPass, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(tNom, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tPrenom, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tAdresse, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tVille, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tTel, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tCp, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tPass2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                            .addComponent(tPass, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                            .addComponent(bAnnuler, javax.swing.GroupLayout.Alignment.LEADING))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tAdresse, tCp, tEmail, tNom, tPrenom, tTel, tVille});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lNom, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(lPrenom, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(lAdresse, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tNom)
                        .addGap(18, 18, 18)
                        .addComponent(tPrenom)
                        .addGap(18, 18, 18)
                        .addComponent(tAdresse)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lCp, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                    .addComponent(tCp))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lVille, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(lTel, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                        .addGap(11, 11, 11)
                        .addComponent(lEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lPass, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lPass2, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                            .addComponent(tPass2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tVille)
                        .addGap(18, 18, 18)
                        .addComponent(tTel)
                        .addGap(11, 11, 11)
                        .addComponent(tEmail)
                        .addGap(69, 69, 69)))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bAnnuler)
                    .addComponent(bValider))
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Action du bouton
     * @param evt
     */
    private void bValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bValiderActionPerformed
        if (erreurForm()){
            ClientBusinessServiceImpl bs = new ClientBusinessServiceImpl();
            Client leClient = new Client();
            leClient.setAdresse(tAdresse.getText());
            leClient.setEmail(tEmail.getText());
            leClient.setNom(tNom.getText());
            leClient.setPrenom(tPrenom.getText());
            leClient.setTelephone(tTel.getText());

            //TODO à décommenter quand les modifs seront faites en base
            leClient.setVille(tVille.getText());
            leClient.setPassword(tPass.getText());
            leClient.setCp(tCp.getText());
            bs.createClient(leClient);
            this.setVisible(false);
            JOptionPane.showMessageDialog(null,"Client enregistré","Enregistrement effectué",JOptionPane.INFORMATION_MESSAGE);
            new Authentification().setVisible(true);
        }
}//GEN-LAST:event_bValiderActionPerformed

    private void bAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAnnulerActionPerformed
       Object[] choix = { "Oui", "Non" };
       int choix_utilisateur=JOptionPane.showOptionDialog(null, "Voulez vous quittez le menu ?", "Quittez le menu",JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,null, choix, choix[0]);
       if (choix_utilisateur==0){
       this.setVisible(false);
       new Authentification().setVisible(true);
       }
       
    }//GEN-LAST:event_bAnnulerActionPerformed

    private void tPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tPassActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tPassActionPerformed

    /**
    * @param args the command line arguments
    */


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAnnuler;
    private javax.swing.JButton bValider;
    private javax.swing.JLabel lAdresse;
    private javax.swing.JLabel lCp;
    private javax.swing.JLabel lEmail;
    private javax.swing.JLabel lNom;
    private javax.swing.JLabel lPass;
    private javax.swing.JLabel lPass2;
    private javax.swing.JLabel lPrenom;
    private javax.swing.JLabel lTel;
    private javax.swing.JLabel lVille;
    private javax.swing.JTextField tAdresse;
    private javax.swing.JTextField tCp;
    private javax.swing.JTextField tEmail;
    private javax.swing.JTextField tNom;
    private javax.swing.JPasswordField tPass;
    private javax.swing.JPasswordField tPass2;
    private javax.swing.JTextField tPrenom;
    private javax.swing.JTextField tTel;
    private javax.swing.JTextField tVille;
    // End of variables declaration//GEN-END:variables

    /**
     * Validation du formulaire
     * @return
     */
     private boolean erreurForm() {
        boolean noError = true;
        String msgErreur = "Le(s) champ(s) suivant(s) est/sont incorrectes :\n";
        if ((this.tAdresse == null) || (this.tAdresse.getText().length()<1)){
            msgErreur+=" - Adresse\n";
            noError = false;
        }

        if ((this.tEmail == null) || (this.tEmail.getText().length()<5) ||
                ((this.tEmail.getText().indexOf("@")==-1))){
            msgErreur+=" - Email\n";
            noError = false;
        }


        if ((this.tNom == null) || (this.tNom.getText().length()<1)){
           msgErreur+=" - Nom\n";
            noError = false;
        }


        if ((this.tPrenom == null) || (this.tPrenom.getText().length()<1)){
            msgErreur+=" - Prénom\n";
            noError = false;
        }

        if ((this.tTel == null) || (this.tTel.getText().length()<10)){
            msgErreur+=" - Téléphone\n";
            noError = false;
        }
        else {
            try {
             Integer.parseInt(tTel.getText());
            } catch (NumberFormatException e){
                msgErreur+=" - Téléphone\n";
                noError = false;
            }
        }

        if ((this.tVille == null) || (this.tVille.getText().length()<1)){
            msgErreur+=" - Ville\n";
            noError = false;
        }


       if ((this.tCp == null) || (this.tCp.getText().length()!=5)){
            msgErreur+=" - Code postal\n";
            noError = false;
        }
        else {
            try {
                Integer.parseInt(tCp.getText());
            } catch (NumberFormatException e){
                msgErreur+=" - Code postal\n";
                noError = false;
            }
             this.tVille.setBackground(Color.WHITE);
        }

        if ((!this.tPass.getText().equals(this.tPass2.getText()))){
            msgErreur+=" - Password\n";
            noError = false;
        }


        if ((this.tPass == null) || (this.tPass.getText().length()<1)){
            msgErreur+=" - Password\n";
            noError = false;
        }

        if ((this.tPass2 == null) || (this.tPass2.getText().length()<1)){
            msgErreur+=" - Password\n";
            noError = false;
        }
        if(!noError){
            JOptionPane.showMessageDialog(null,msgErreur,"Erreur formulaire",JOptionPane.WARNING_MESSAGE);
        }
        return noError;
    }
}
